/*
 * CasesOutput.cpp
 *
 *  Created on: 27/11/2011
 *      Author: godinho
 */

#include <evolutive/output/CasesOutput.h>
#include <controller/Case.h>

#define fout (*mOut)
#define opentag(tag)	printTabs(); fout << tag << "\n"; otag()
#define closetag(tag)   ctag(); printTabs(); fout << tag << "\n"
#define tag printTabs(); fout

CasesOutput::CasesOutput(string filename) : mFileName(filename) {
	depth = 0;
}

CasesOutput::~CasesOutput() {

}

void CasesOutput::open() {
	mOut = new ofstream(mFileName.c_str());
	opentag("<cases>");
}

void CasesOutput::close() {
	closetag("</cases>");
	mOut->close();
}

void CasesOutput::appendGeneration(int generationId, vector<Case*> generation) {
	opentag("<generation id = \"" << generationId <<"\">");

	for(unsigned int i = 0; i < generation.size(); i++) {
		appendCase(generation[i]);
	}

	closetag("</generation>");
}

void CasesOutput::appendCase(Case* c) {
	opentag("<case "
			<< "id=\"" << c->getId() << "\" "
			<< "fitness=\"" << c->getFitness() << "\" "
			<< ">");

	closetag("</case>");
}

void CasesOutput::otag() {
	depth++;
}

void CasesOutput::ctag() {
	depth--;
}

void CasesOutput::printTabs() {
	for(int i = 0; i < depth; i++) {
		fout << "    ";
	}
}
